/*
 * Lab7_Terminal.c
 *
 * Created: 4/1/2014 8:52:24 AM
 *  Author: mummm2
 */ 
//UBRR -	Baud rate. Rate at which the data will be transmitted. Split into H/L
//UDR -		Data register; Stores send/received data.
//UCSRA -	Controls various settings for the USART connections
//UCSR0A = (0<<RXC0) | (0<<TXC0) | (0<<UDRE0);  //Flags for Rec/Trans/Data
//UCSR0B = (0<<RXCIE0) | (0<<TXCIE0) | (0<<UDRIE0) | (0<<RXEN0) | (0<<TXEN0) | (0<<UCSZ02) | (0<<RXB80) | (0<<TXB80);
//UCSR0C = (0<<UMSEL01) | (0<<UMSEL00) | (0<<UPM01) | (0<<USBS0) | (0<<UCSZ01) | (0<<UCSZ00) | (0<<UCPOL0);
/*Baud Rate
A, NM = fosc/(16(UBRRn+1)
A, DS = fosc/(16(UBRRn+1)
S, MM = fosc/(16(UBRRn+1)
*/

#include <avr/io.h>
#include <avr/interrupt.h> //used for interrupts

//initiate the setup to send data through TX pin via serial
void usart_init(void) {
	DDRB=0xff;
	UCSR0B = (1<<TXEN0) | (0<<RXEN0);
	UCSR0C = (1<<UCSZ01) | (1<<UCSZ00) | (0<<UMSEL01) | (0<<UMSEL00);
	UBRR0 = 0x33;
}

//send one char at a time to the terminal screen
void usart_send(unsigned char ch){
	while(!(UCSR0A &(1<<UDRE0)));
	UDR0 = ch;
}

int main(void) {
	DDRD = 0xff;	//set all of portB as output, used to turn on each 7segment individually
	
	//data will be L-Justified, PC5 for analog reading, 1.1V internal voltage ref with 10uf cap
	ADMUX = (1<<REFS1) | (1<<REFS0) | (1<<ADLAR) | (0<<MUX3) | (1<<MUX2) | (0<<MUX1) | (1<<MUX0);
	//make ADC enable and select ck/128
	ADCSRA = (1<<ADEN) | (0<<ADSC) | (0<<ADATE) | (0<<ADIF) | (0<<ADIE) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0);
	
	//enable interrupts on t1, enable OutputCompareA interrupt
	TIMSK1 = (1<<ICIE1) | (1<<OCIE1A) | (0<<TOIE1);
	sei();				//enable interrupts 
	OCR1A = 0x7a12;		//Delay time, 1 sec
	TCCR1A = (0<<WGM11) | (0<<WGM10);	//CTC
	TCCR1B = (0<<WGM13) | (1<<WGM12) | (1<<CS12) | (0<<CS11) | (0<<CS10);  //N=256
		
	usart_init();	//call to initialize sending of the data
	
	while(1) {
	//run forever
	}
	return 0;
}

//interrupt is called when timer1 counts for 1 sec. 
ISR(TIMER1_COMPA_vect) {
	int temp1=255;		//tens place for temperature
	int temp2=255;		//ones place for temperature
	int temp3=255;		//decimal place for temperature
	unsigned char point='.';		//decimal point
	unsigned char nl[8]="        ";	//spacing for between temperatures displayed
	int i;				//used for looping spaces
	
	ADCSRA |= (1<<ADSC);			//Start Conversion
	while((ADCSRA&(1<<ADIF))==0);	//wait for end of conversion
	
	temp1=ADCH;		//capture ADC
	temp1 *= 4.444;	//due to low reference voltage, number needs to be mult
	
	temp3 = temp1%10 + 0x30;		//calc decimal place, +0x30 is for display purposes
	temp2 = (temp1/10)%10 + 0x30;	//calc ones place, +0x30 is for display purposes
	temp1 = (temp1/100)%10 + 0x30;	//calc tens place, +0x30 is for display purposes
	
	usart_send(temp1);		//send tens place
	usart_send(temp2);		//send ones place
	usart_send(point);		//send decimal point
	usart_send(temp3);		//send decimals place
	i=0;	//loop used to send spaces
	while(i<8) {
		usart_send(nl[i]);
		i++;
	}	
}